<?php
namespace app\shop\controller\admin;

use  app\admin\library\AdminController;

class Model extends AdminController{

    public function onIndex(){
        $this->pageTitle = '模型管理';
        $this->pageIcon = 'fa-cubes';
        $this->power('shop.setting.model');

        $model = $this->model('Model');

        $this->set('modelType',$model->getType());
        
        return $this->display();
    }

    public function onList(){
        $this->power('shop.setting.model');
        $type = $this->post('type',$this->check()->type('string'));
        $keyword =  $this->post('keyword',$this->check()->type('string'));
        $model = $this->model('Model');
        $pageCurrent = $this->post('page_current',$this->check('当前页')->default(1)->type('int'));

        $model->where('type','<>','globalparam');
        
        if ($type){
            $model->where('type',$type);
        }

        if ($keyword){
            $model->where('title','like','%'.$keyword.'%');
        }

        $re['list'] = $model->page($pageCurrent,20)->order('model_id','DESC')->getData();
        $re['count'] = $model->pageCount;
        $re['total'] = $model->total;

        return $re;
    }


   
    public function onEdit(){
        $this->power('shop.setting.model');
        $data['title'] = $this->post('title',$this->check()->type('string')->required());
        $data['type'] = $this->post('type',$this->check()->type('string')->required());
        $model_id =  $this->post('model_id',$this->check()->type('int'));

        $model = $this->model('Model');

        if ($model_id){
            if($model->updateId($model_id,$data)){
                $this->log('修改模型');
                return true;
            }
        }else{
            if ($model->insert($data)){
                $this->log('添加模型');
                return true;
            }
        }

        throw new \Exception('保存失败');

    }

    public function onDel(){
        $this->power('shop.setting.model');
        $model_id =  $this->post('model_id',$this->check()->type('int')->required());

        $model = $this->model('Model');

        //判断栏目里面是否正在使用

        if ($model->deleteId($model_id)){
            $this->log('删除模型');
            return true;
        }

        throw new \Exception('删除失败');

    }

    public function onCold(){
        $this->power('shop.setting.model');
        $model_id =  $this->post('id',$this->check()->type('int')->required());
        $cold = $this->post('cold',$this->check()->type('int')->default(0));
        $model = $this->model('Model');
        if ($model->updateId($model_id,['cold'=>$cold])){
            
            return true;
        }

        throw new \Exception('修改失败');
    }

    public function onParamList(){
        
        $model_id =  $this->post('model_id',$this->check()->type('int')->required());


        $modelParam = $this->model('ModelParam');
        $modelParam->where('model_id',$model_id);
        $modelParam->order('sort','desc');

        $list = $modelParam->getData();

        for ($i=0;$i<count($list);$i++){
            try{
                $list[$i]['option'] = json_decode($list[$i]['option']);
            }catch(\Exception $e){
                $list[$i]['option'] = [];
            }

            try{
                $list[$i]['props'] = json_decode($list[$i]['props']);
            }catch(\Exception $e){
                $list[$i]['props'] = [];
            }
            
        }


        return $list;
    }

    public function onSaveParam(){
        $this->power('shop.setting.model');
        $model_id =  $this->post('model_id',$this->check()->type('int')->required());
        $param_id =  $this->post('param_id',$this->check()->type('int'));

        $data= [];
        $data['name'] = $this->post('name',$this->check()->type('string')->required());
        $data['model_id'] = $model_id;
        $data['title'] = $this->post('title',$this->check()->type('string')->required());
        $data['type'] = $this->post('type',$this->check()->type('string')->required());
        $data['type_title'] = $this->post('type_title',$this->check()->type('string')->required());
        $data['sort'] = '';
        $data['defaultValue'] = $this->post('defaultValue',$this->check()->type('string'));
        $data['required'] = $this->post('required',$this->check()->type('int'));
  
        $option =  $this->post('option',[]);
        $props =  $this->post('props',[]);

        $model =  $this->model('Model');

       
        
        $modelData = $model->getId($model_id);
        if (!$modelData){
            throw new \Exception('模型不存在');
        }
        
        $reservedField  = $model->reservedField( $modelData['type']);
        
        if (in_array($data['name'],$reservedField)){
            throw new \Exception($data['name'].' 不能使用,请修改名称');
        }

        try{
            $data['option'] = json_encode($option);
            $data['props'] = json_encode($props);
        }catch(\Exception $e){
        }
        
        $modelParam = $this->model('ModelParam');

        if ($param_id) {

            unset($data['name']);

            if ($modelParam->updateId($param_id,$data)){
                unset($data['option']);
                $this->log('修改参数',$data);
                return true;
            }
            
        }else{

            if ($modelParam->where('model_id',$model_id)->where('name',$data['name'])->count()>0){
                throw new \Exception('参数名重复');
            }

            if ($modelParam->insert($data)){
                unset($data['option']);
                $this->log('添加参数',$data);
                return true;
            }
        }
    }


    public function onDelParam(){
        $this->power('shop.setting.model');
        //$model_id =  $this->post('model_id',$this->check()->type('int')->required());
        $param_id =  $this->post('param_id',$this->check()->type('int')->required());


        $modelParam = $this->model('ModelParam');

        $data = $modelParam->getId($param_id);

        if (!$data){
            throw new \Exception('参数不存在');
        }

        if ($modelParam->deleteId($param_id)){
            unset($data['option']);
            $this->log('删除参数',$data);
            return true;
        }
    }

    public function onParamSort(){
        $this->power('shop.setting.model');
        $model_id =  $this->post('model_id',$this->check()->type('int')->required());
        $ids = $this->post('ids',$this->check()->type('array')->required());

        if (count($ids)<2){
            throw new \Exception('排序失败');
        }

        $modelParam = $this->model('ModelParam');

        $modelParam->where('model_id',$model_id)->switchSort($ids,'sort','desc');

        return true;
    }
}